package com.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exam.mapper.DepartmentMapper;
import com.exam.pojo.entity.Department;
import com.exam.pojo.entity.Position;
import com.exam.pojo.mode.DepartVo;
import com.exam.pojo.mode.DepartmentVo;
import com.exam.service.DepartmentService;
import com.exam.service.PositionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private PositionService positionService;

    /**
     * 查询部门列表
     * @return
     */
    @Override
    public List<DepartmentVo> findAllList() {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getDelFlag,1);
        List<Department> departmentList = departmentMapper.selectList(queryWrapper);
        return copyDepartmentVoList(departmentList);
    }

    /**
     * 分页 查询部门列表
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<DepartVo> listDepartVo(int page, int size) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getDelFlag,1);
        List<Department> departmentList = departmentMapper.selectList(queryWrapper);
        return copyDepartVoList(departmentList);
    }

    /**
     * 添加部门
     * @param department
     */
    @Override
    public void addDepartment(Department department) {
        departmentMapper.insert(department);
    }

    /**
     * 删除部门
     * @param departmentId
     */
    @Override
    public void delDepartment(Integer departmentId) {
        Department department = new Department();
        department.setId(departmentId);
        department.setDelFlag(0);
        departmentMapper.updateById(department);
    }

    /**
     * 批量删除部门
     * @param departmentIdList
     */
    @Override
    public void delAllDepartment(List<Integer> departmentIdList) {
        for (Integer departmentId : departmentIdList) {
            this.delDepartment(departmentId);
        }
    }

    /**
     * 根据名字查询部门
     * @param name
     * @return
     */
    @Override
    public List<DepartVo> findDepartmentByName(String name) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getName,name);
        queryWrapper.eq(Department::getDelFlag,1);
        List<Department> departmentList = departmentMapper.selectList(queryWrapper);
        return copyDepartVoList(departmentList);
    }

    /**
     *
     * @param department
     */
    @Override
    public void editDepartment(Department department) {

    }

    private List<DepartVo> copyDepartVoList(List<Department> departmentList) {
        List<DepartVo> departVoList = new ArrayList<>();
        for (Department department : departmentList) {
            departVoList.add(copyDepartVo(department));
        }
        return departVoList;
    }

    private DepartVo copyDepartVo(Department department) {
        //变量替换
        DepartVo departVo = new DepartVo();
        BeanUtils.copyProperties(department,departVo);
        // 根据parentId找到父公司
        Integer parentId = department.getParentId();
        if(parentId == null) return departVo;
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getId,parentId);
        queryWrapper.eq(Department::getDelFlag,1);
        queryWrapper.last("limit 1");
        Department parentDepartment = departmentMapper.selectOne(queryWrapper);
        departVo.setParentName(parentDepartment.getName());
        return departVo;
    }


    private List<DepartmentVo> copyDepartmentVoList(List<Department> departmentList) {
        List<DepartmentVo> departmentVoList = new ArrayList<>();
        for (Department department : departmentList) {
            departmentVoList.add(copyDepartmentVo(department));
        }
        return departmentVoList;
    }

    private DepartmentVo copyDepartmentVo(Department department) {
        //1.查询部门 departmentList
        DepartmentVo departmentVo = new DepartmentVo();
        BeanUtils.copyProperties(department,departmentVo);
        //2. 根据 departmentId 查询岗位positionList
        Integer departmentId = department.getId();
        List<Position> positionList = positionService.findPositionListByDepartmentId(departmentId);
        departmentVo.setPositionList(positionList);
        return departmentVo;
    }


}
